package com.rem.mp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rem.mp.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * mp activeRecord 数据访问设计模式
 * 应用Active Record 时，每一个类的实例对象唯一对应一个数据库表的一行(一对一关系)。你只需继承一个abstract Active Record 类就可以使用该设计模式访问数据库
 * 实体类继承Model<T>  仍然要写mapper  但是在应用中不需要导入
 *
 * @Author Rem
 * @Date 2020-02-29
 */

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class MyApplicationTest4 {


    /**
     * 新增
     * INSERT INTO tb_user (user_name, age) VALUES (?, ?)
     */
    @Test
    public void testInsert() {

        User user = new User("xiahoudun", 24);
        boolean insert = user.insert();

        //B当原有记录存在时（id存在）则是更新操作
        // boolean update = user.insertOrUpdate();

        System.err.println(insert);
    }


    /**
     * 删除
     * DELETE FROM tb_user WHERE id = ?
     */
    @Test
    public void testDelete() {

        User user = new User();
        user.setId(13L);
        //A根据传入的实体的id删除
        //boolean delete = user.deleteById();

        //B直接根据id删除
        //boolean delete2 = new User().deleteById(13L);

        //C根据传入的条件删除
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name", "xiahoudun");
        //DELETE FROM tb_user WHERE (user_name = ?)
        boolean delete3 = new User().delete(wrapper);

        System.err.println(delete3);
    }

    /**
     * 修改
     */
    @Test
    public void testUpdate() {
        //构建实体类修改
        User user = new User();
        //修改的记录
        user.setId(15L);
        //修改内容
        user.setName("夏侯惇");
        user.setAge(34);
        //UPDATE tb_user SET name = ?, age = ? WHERE id = ?
        //boolean update = user.updateById();

        //根据条件修改
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        User user1 = new User();
        //修改的内容
        user1.setUserName("xiahoudun");
        user1.setPassword("123");

        //修改条件
        wrapper.eq("id", 15L);
        //UPDATE tb_user SET name = ?, age = ? WHERE (id = ?)
        boolean update1 = user.update(wrapper);

        System.err.println(update1);
    }

    /**
     * 查询
     * SELECT id, email AS mail, name, user_name, age FROM tb_user WHERE id = ?
     */
    @Test
    public void testSelect() {
        User user = new User();
        user.setId(15L);
        //根据实体类的id查询
        User user1 = user.selectById();

        //直接输入id查询
        User user2 = new User().selectById(15L);
        System.out.println(user2);
    }

    /**
     * 其他查询
     */
    @Test
    public void testOther() {

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.gt("age", 24);
        //查询记录数
        //SELECT COUNT(1) FROM tb_user WHERE (age > ?)
        // Integer integer = new User().selectCount(wrapper);
        //  System.err.println(integer);


        //查询所有
        //SELECT id, email AS mail, name, user_name, age FROM tb_user
        // List<User> users = new User().selectAll();
        //  users.forEach(System.err::println);


        //根据条件查询一个  查询到多个取第一个
        //SELECT id, email AS mail, name, user_name, age FROM tb_user WHERE (age = ?)
        QueryWrapper<User> wrapper2 = new QueryWrapper<>();
        wrapper2.eq("age", 24);
        // User user2 = new User().selectOne(wrapper2);
        // System.err.println(user2);


        //分页查询
        //SELECT id, email AS mail, name, user_name, age FROM tb_user WHERE (age > ?) LIMIT ?,?
        IPage<User> userIPage = new User().selectPage(new Page<>(2L, 4), wrapper);
        System.err.println(userIPage.getRecords().toString());
    }

}